package com.etlmaster.core;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;

import com.etlmaster.executor.utils.LogWriter;

/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
public class Configuration {

	private static Configuration conf = new Configuration();
	private Properties prop = null;
	
	
	private String propsPattern = "classpath*:conf/etlmaster_*.properties";
	
	
	private Configuration(){
		prop = new Properties();
		
	}
	
	public static Configuration getInstance(){
		return conf;
	}
	
	public String getProperty(String key){
		return prop.getProperty(key);
	}
	
	
	
	private void load(Resource[] resources){
		for (Resource resource : resources) {
			if(resource!=null  && resource.isReadable() ){
				try {
					prop.load(new InputStreamReader(resource.getInputStream(),"utf-8"));
					LogWriter.addLog("INFO","{}加载成功",resource.getFilename());
				} catch (IOException e) {
					e.printStackTrace();
					LogWriter.addLog("WARN","{}加载失败",resource.getFilename());
				}
			}
		}
	}
	
	public void loadClasspathProperties(ApplicationContext applicationContext){
		LogWriter.addLog("INFO","加载{}配置文件",propsPattern);
		try {
			load(applicationContext.getResources(Configuration.getInstance().propsPattern));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}
